package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.mapper.SysRoleMapper;
import com.example.teachingSystem.pojo.entity.ConstantEnum;
import com.example.teachingSystem.pojo.entity.SysRole;
import com.example.teachingSystem.pojo.form.role.AddRoleForm;
import com.example.teachingSystem.pojo.form.role.ChangeRoleInfoForm;
import com.example.teachingSystem.pojo.form.role.RolePageQuery;
import com.example.teachingSystem.pojo.vo.OptionVo;
import com.example.teachingSystem.pojo.vo.RoleInfoVo;
import com.example.teachingSystem.service.SysRoleService;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 86147
 * @description 针对表【sys_role(角色表)】的数据库操作Service实现
 * @createDate 2023-11-27 21:43:54
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Result<RoleInfoVo> getRoleById(Long id) {
        SysRole sysRole = sysRoleMapper.selectById(id);
        if (sysRole == null) {
            return Result.build(null, 500, "该角色不存在");
        }
        RoleInfoVo roleInfoVo = new RoleInfoVo();
        BeanUtils.copyProperties(sysRole, roleInfoVo);
        return Result.ok(roleInfoVo);
    }


    @Override
    public PageResult<RoleInfoVo> getRolePage(RolePageQuery rolePageQuery) {
        Page<SysRole> rolePage = new Page<>(rolePageQuery.getPageNum(), rolePageQuery.getPageSize());
        IPage<RoleInfoVo> roleInfoVoIPage = sysRoleMapper.getRolePage(rolePage, rolePageQuery);
        return PageResult.success(roleInfoVoIPage);
    }


    @Override
    public Result<String> addRole(AddRoleForm addRoleForm) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(addRoleForm, sysRole);
        this.save(sysRole);
        return Result.ok(null, "添加成功");
    }

    @Override
    public Result<String> updateRole(ChangeRoleInfoForm changeRoleInfoForm) {
        SysRole role = sysRoleMapper.selectById(changeRoleInfoForm.getId());
        if (role == null) {
            return Result.build(null, 500, "角色不存在");
        }
        BeanUtils.copyProperties(changeRoleInfoForm, role);
        this.updateById(role);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> changeRoleStatus(Long id, boolean isDisabled) {
        SysRole role = sysRoleMapper.selectById(id);
        if (role == null) {
            return Result.build(null, 500, "角色不存在");
        }
        if(isDisabled){
            if(Objects.equals(role.getStatus(), ConstantEnum.STATUS_DISABLED.getValue())){
                return Result.build(null,500,"角色已停用中");
            }
            role.setStatus(ConstantEnum.STATUS_DISABLED.getValue());
            this.updateById(role);
            return Result.ok(null,"角色已成功停用");
        }else{
            if(Objects.equals(role.getStatus(), ConstantEnum.STATUS_NORMAL.getValue())){
                return Result.build(null,500,"角色正处于正常状态");
            }
            role.setStatus(ConstantEnum.STATUS_NORMAL.getValue());
            this.updateById(role);
            return Result.ok(null,"角色已恢复到正常状态");
        }

    }


    @Override
    public Result<String> changeRoleDeleted(Long id, boolean isDeleted){
        SysRole role = sysRoleMapper.selectById(id);
        if (role == null) {
            return Result.build(null, 500, "角色不存在");
        }
        if(isDeleted){
            if(Objects.equals(role.getDeleted(), ConstantEnum.IS_DELETED.getValue())){
                return Result.build(null,500,"角色已删除");
            }
            this.removeById(role);
            return Result.ok(null,"角色已删除");
        }else{
            if(Objects.equals(role.getDeleted(), ConstantEnum.NOT_DELETED.getValue())){
                return Result.build(null,500,"角色未删除");
            }
            role.setStatus(ConstantEnum.NOT_DELETED.getValue());
            this.updateById(role);
            return Result.ok(null,"角色已恢复");
        }
    }

    @Override
    public Result<List<OptionVo>> getRoleOptions(){
        List<OptionVo> optionVos = sysRoleMapper.getRoleOptions();
        return Result.ok(optionVos);
    }

}




